package cn.jiyun.util;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class GeneratorUtils {
    public static void main(String[] args) {

        FastAutoGenerator.create(new DataSourceConfig.Builder("jdbc:mysql://127.0.0.1:3306/h2206b_demo06", "root", "aaaa")
                .dbQuery(new MySqlQuery())
                // .schema("mybatis-plus")
                .typeConvert(new MySqlTypeConvert())
                .keyWordsHandler(new MySqlKeyWordsHandler()))
                // 全局配置
                .globalConfig((scanner, builder) -> builder.author("lishao")
                        // 是否生成swagger
                        //.enableSwagger()
                        .outputDir("D://code/"))
                // 包配置
                .packageConfig((scanner, builder) -> builder
                        .parent("cn.jiyun") // 设置父包名
                        .entity("pojo")
                        .service("service")
                        .mapper("mapper")
                        //.controller("controller")
                        .pathInfo(Collections.singletonMap(OutputFile.xml, "D://code/")))
                // 策略配置
                .strategyConfig((scanner, builder) -> builder.addInclude(getTables("rent_user,rent_income"))
                        .controllerBuilder().enableRestStyle().enableHyphenStyle()
                        .entityBuilder().enableLombok()
                        .build())
                .execute();
    }

    // 处理 all 情况
    protected static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }
}
